package com.tf_cut.backend.util;

import com.tf_cut.backend.agent.vo.AgentCashOrderVo;
import com.tf_cut.backend.datadic.vo.DatadicVo;
import com.tf_cut.backend.mapper.DatadicMapper;
import com.tf_cut.backend.person.vo.PersonAndAccountVo;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Component
public class SendMail {
	@Autowired
	private JavaMailSender mailSender;

	@Autowired
	private DatadicMapper datadicMapper;

	/**
	 * 无内容认证通知
	 */
	public void registerMailSend() {
		DatadicVo datadicVo = new DatadicVo();
		datadicVo.setDatadicCode("MAIL_SEND");
		datadicVo.setDatadicKey("REGISTER_MAIL");
		List<DatadicVo> list = datadicMapper.queryDatadic(datadicVo);
		if (!list.isEmpty() && StringUtils.isNotBlank(list.get(0).getDatadicValue())) {
			this.send(list.get(0).getDatadicValue(), "新认证通知", "有一个新的认证需审核");
		}
	}

	/**
	 * 有内容认证通知
	 * 
	 * @param avo
	 */
	public void registerInfoMailSend(PersonAndAccountVo avo) {
		DatadicVo datadicVo = new DatadicVo();
		datadicVo.setDatadicCode("MAIL_SEND");
		datadicVo.setDatadicKey("REGISTER_MAIL");
		List<DatadicVo> list = datadicMapper.queryDatadic(datadicVo);
		if (!list.isEmpty() && StringUtils.isNotBlank(list.get(0).getDatadicValue())) {
			DatadicVo data = list.get(0);
			String[] split = data.getDatadicValue().split(",");
			for (int i = 0; i < split.length; i++) {
				this.send(split[i], "新认证通知", "有一个新的认证需审核   姓名:" + avo.getPersonName() + " \n手机号：" + avo.getMobile()
						+ " \n身份证：" + avo.getCardCode());
			}
		}
	}

	/**
	 * 无内容充值审核
	 */
	public void chargeMailSend() {
		DatadicVo datadicVo = new DatadicVo();
		datadicVo.setDatadicCode("MAIL_SEND");
		datadicVo.setDatadicKey("CHARGE_MAIL");
		List<DatadicVo> list = datadicMapper.queryDatadic(datadicVo);
		if (!list.isEmpty() && StringUtils.isNotBlank(list.get(0).getDatadicValue())) {
			this.send(list.get(0).getDatadicValue(), "有一个新的充值需审核", "有一个新的充值需审核");
		}
	}

	/**
	 * 有内容充值审核
	 * 
	 * @param sellerName
	 * @param investAmount
	 * @param createTime
	 */
	public void chargeInfoMailSend(String sellerName, String investAmount, String createTime) {
		DatadicVo datadicVo = new DatadicVo();
		datadicVo.setDatadicCode("MAIL_SEND");
		datadicVo.setDatadicKey("CHARGE_MAIL");
		List<DatadicVo> list = datadicMapper.queryDatadic(datadicVo);
		if (!list.isEmpty() && StringUtils.isNotBlank(list.get(0).getDatadicValue())) {
			DatadicVo data = list.get(0);
			String[] split = data.getDatadicValue().split(",");
			for (int i = 0; i < split.length; i++) {
				this.send(split[i], "有一个新的充值需审核",
						"商户名：" + sellerName + "\n充值金额：" + investAmount + "\n提交时间" + createTime);
			}
		}
	}

	/**
	 * 无内容发放审核
	 */
	public void distributeMailSend() {
		DatadicVo datadicVo = new DatadicVo();
		datadicVo.setDatadicCode("MAIL_SEND");
		datadicVo.setDatadicKey("DISTRIBUTE_MAIL");
		List<DatadicVo> list = datadicMapper.queryDatadic(datadicVo);
		if (!list.isEmpty() && StringUtils.isNotBlank(list.get(0).getDatadicValue())) {
			this.send(list.get(0).getDatadicValue(), "有一个新的发放需审核", "有一个新的发放需审核");
		}
	}

	/**
	 * 有内容发放审核
	 * 
	 * @param name
	 * @param money
	 * @param time
	 */
	public void distributeInfoMailSend(String name, String money, String time) {
		DatadicVo datadicVo = new DatadicVo();
		datadicVo.setDatadicCode("MAIL_SEND");
		datadicVo.setDatadicKey("DISTRIBUTE_MAIL");
		List<DatadicVo> list = datadicMapper.queryDatadic(datadicVo);
		if (!list.isEmpty() && StringUtils.isNotBlank(list.get(0).getDatadicValue())) {
			DatadicVo data = list.get(0);
			String[] split = data.getDatadicValue().split(",");
			for (int i = 0; i < split.length; i++) {
				this.send(split[i], "有一个新的发放需审核", "商户名：" + name + "\n发放金额：" + money + "\n提交时间：" + time);
			}
		}
	}

	/**
	 * 无内容提现审核
	 */
	public void withdrawMailSend() {
		DatadicVo datadicVo = new DatadicVo();
		datadicVo.setDatadicCode("MAIL_SEND");
		datadicVo.setDatadicKey("WITHDRAW_MAIL");
		List<DatadicVo> list = datadicMapper.queryDatadic(datadicVo);
		if (!list.isEmpty() && StringUtils.isNotBlank(list.get(0).getDatadicValue())) {
			this.send(list.get(0).getDatadicValue(), "有一个新的提现需审核", "有一个新的提现需审核");
		}
	}

	/**
	 * 有内容提现审核
	 */
	public void withdrawInfoMailSend(AgentCashOrderVo agentCashOrder) {
		DatadicVo datadicVo = new DatadicVo();
		datadicVo.setDatadicCode("MAIL_SEND");
		datadicVo.setDatadicKey("WITHDRAW_MAIL");
		List<DatadicVo> list = datadicMapper.queryDatadic(datadicVo);
		if (!list.isEmpty() && StringUtils.isNotBlank(list.get(0).getDatadicValue())) {
			DatadicVo data = list.get(0);
			String[] split = data.getDatadicValue().split(",");
			for (int i = 0; i < split.length; i++) {
				this.send(split[i], "有一个新的提现需审核",
						"商户名：" + agentCashOrder.getAccountName() + "\n手机号：" + agentCashOrder.getMobile() + "\n提现金额："
								+ agentCashOrder.getCashAmount() + "\n提交时间：" + agentCashOrder.getApplyTime());
			}
		}
	}

	/**
	 * 无内容发票申请
	 */
	public void billMailSend() {
		DatadicVo datadicVo = new DatadicVo();
		datadicVo.setDatadicCode("MAIL_SEND");
		datadicVo.setDatadicKey("BILL_MAIL");
		List<DatadicVo> list = datadicMapper.queryDatadic(datadicVo);
		if (!list.isEmpty() && StringUtils.isNotBlank(list.get(0).getDatadicValue())) {
			this.send(list.get(0).getDatadicValue(), "有一个新的发票申请需审核", "有一个新的发票申请需审核");
		}
	}

	/**
	 * 有内容发票申请
	 * 
	 * @param name
	 * @param money
	 * @param time
	 */
	public void billInfoMailSend(String name, String money, String time) {
		DatadicVo datadicVo = new DatadicVo();
		datadicVo.setDatadicCode("MAIL_SEND");
		datadicVo.setDatadicKey("BILL_MAIL");
		List<DatadicVo> list = datadicMapper.queryDatadic(datadicVo);
		if (!list.isEmpty() && StringUtils.isNotBlank(list.get(0).getDatadicValue())) {
			DatadicVo data = list.get(0);
			String[] split = data.getDatadicValue().split(",");
			for (int i = 0; i < split.length; i++) {
				this.send(split[i], "有一个新的发票申请需审核", "\n商户名:" + name + "\n开票金额：" + money + "\n提交时间：" + time);
			}
		}
	}

	public void send(String to, String subject, String context) {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom("notice@xydtax.cn");
		message.setTo(to);
		message.setSubject(subject);
		message.setText(context);
		mailSender.send(message);
	}

	/**
	 * 发票预警
	 * 
	 * @param name
	 * @param money
	 * @param time
	 */
	public void invoiceAlertMailSend(String name, String money, String time) {
		String[] split = name.split(",");
		for (int i = 0; i < split.length; i++) {
			this.send(split[i], "有一个新的发票预警", "\n发票" + money + "已到达预警" + "\n预警时间：" + time);
		}
	}

	/**
	 * 系统预警(网商余额对比)
	 * @param sellerName
	 * @param beforeMoney
	 * @param afterMoney
	 */
	public void sellerMoneyMailSend(String sellerName, int beforeMoney, int afterMoney) {
		DatadicVo datadicVo = new DatadicVo();
		datadicVo.setDatadicCode("MAIL_SEND");
		datadicVo.setDatadicKey("MONEY_MAIL");
		List<DatadicVo> list = datadicMapper.queryDatadic(datadicVo);
		if (!list.isEmpty() && StringUtils.isNotBlank(list.get(0).getDatadicValue())) {
			DatadicVo data = list.get(0);
			String[] split = data.getDatadicValue().split(",");
			for (int i = 0; i < split.length; i++) {
				this.send(split[i], "有一个新的系统预警", "\n商户名" + sellerName + "金额对比网商不正确" + "\n系统余额：" + beforeMoney +"网商余额：" +afterMoney);
			}
		}

	}
}
